package com.cqie.secondhandtransaction.controller;

import com.cqie.secondhandtransaction.DTO.CallBackDTO;
import com.cqie.secondhandtransaction.DTO.DataList;
import com.cqie.secondhandtransaction.DTO.OrderDTO;
import com.cqie.secondhandtransaction.DTO.OrderInfoVO;
import com.cqie.secondhandtransaction.entity.OrderInfo;
import com.cqie.secondhandtransaction.entity.ProductInfo;
import com.cqie.secondhandtransaction.enums.StatusEnum;
import com.cqie.secondhandtransaction.exception.BusinessException;
import com.cqie.secondhandtransaction.model.Result;
import com.cqie.secondhandtransaction.service.OrderInfoService;
import com.github.xiaoymin.knife4j.core.util.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gdy
 * @since 2025-02-10
 */
@RestController
@RequestMapping("/orderInfo")
@Api(value = "订单管理", tags = {"订单模块"})
@Slf4j
public class OrderInfoController extends BaseController {

    @Autowired
    private OrderInfoService orderService;

    @GetMapping("/list")
    @ApiOperation(value = "订单列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<DataList<OrderInfoVO>> orderList(@RequestParam(value = "status", required = false) String status,
                                                   @RequestParam(value = "isSeller") boolean isSeller){
        return response(orderService.orderList(status, isSeller));
    }

    @PostMapping("/start")
    @ApiOperation(value = "发起订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<?> startOrder(@RequestBody OrderDTO dto) {
        orderService.startOrder(dto);
        return response("ok");
    }

    @DeleteMapping("/update")
    @ApiOperation(value = "删除订单", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<Void> deleteOrder(@RequestParam(value = "orderCode") String orderCode) {
        orderService.deleteOrder(orderCode);
        return response();
    }

    @PostMapping("/status")
    @ApiOperation(value = "发起订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<?> updateOrderStatus(@RequestBody OrderDTO dto) {
        Assert.isTrue(Objects.isNull(dto.getStatus()), "状态不能为空");
        Assert.isTrue(Objects.isNull(dto.getId()), "ID不能为空");
        orderService.updateOrderStatus(dto);
        return response("ok");
    }

}
